﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 生产计划批量打印工具
{
    public struct NodeData
    {
        public string NodeID;
        public string ParentNodeID;
        public string NodeName;
        public string LastLogic;
        public string Field;
        public string FieldLogic;
        public string Value;
    }

    public partial class frmJhTypeEditor : Form
    {
        bool bEditMode = false;
        string strTypeID;
        string strConnectionString = "Data Source=@ServerAddress;Initial Catalog=@DatabaseName;Persist Security Info=True;User ID=sa;Password=856518armyman";
        SqlTools.SqlTools sqt;

        public frmJhTypeEditor(string strTmpTypeID)
        {
            InitializeComponent();
            strTypeID = strTmpTypeID;
            txtTypeID.Text = strTypeID;
            bEditMode = true;
            sqt = new SqlTools.SqlTools(strConnectionString.Replace("@ServerAddress", Properties.Settings.Default.ServerAddress)
                                                                                        .Replace("@DatabaseName", "DefualtDatabase"));
            LoadJhTypeLogicTree();
        }

        public frmJhTypeEditor()
        {
            InitializeComponent();
            sqt = new SqlTools.SqlTools(strConnectionString.Replace("@ServerAddress", Properties.Settings.Default.ServerAddress)
                                                                            .Replace("@DatabaseName", "DefualtDatabase"));
        }

        private void LoadJhTypeLogicTree()
        {
            //如果是编辑模式的话，读取分类逻辑树
            AddNodes(null, string.Empty);
        }

        private void AddNodes(TreeNode tnParentNode, string strParentNodeID)
        {
            string strSelectTypeLogics = "select NodeID 节点ID,ParentNodeID 父节点ID,NodeName 节点名称,LastLogic 节点逻辑,Field 字段,FieldLogic 字段逻辑,Value 值 from 计划分类逻辑表 where TypeID=@TypeID and ParentNodeID='@ParentNodeID' order by NodeID";
            if (strParentNodeID.Equals(string.Empty))
            {
                strSelectTypeLogics = strSelectTypeLogics.Replace("@TypeID", strTypeID)
                                                                                 .Replace("='@ParentNodeID'", " is null");
            }
            else
            {
                strSelectTypeLogics = strSelectTypeLogics.Replace("@TypeID", strTypeID)
                                                                                 .Replace("@ParentNodeID", strParentNodeID);
            }
            DataTable tmpdt = sqt.GetDataTable(strSelectTypeLogics);
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    string strNodeID = GetString(tmpdt.Rows[i]["节点ID"]);
                    TreeNode tmptn = new TreeNode();
                    if (GetString(tmpdt.Rows[i]["节点名称"]).Equals(string.Empty))
                    {
                        tmptn.Text = GetString(tmpdt.Rows[i]["节点逻辑"]) + " " + GetString(tmpdt.Rows[i]["字段"]) + " " + GetString(tmpdt.Rows[i]["字段逻辑"]) + " " + GetString(tmpdt.Rows[i]["值"]);
                    }
                    else
                    {
                        tmptn.Text = GetString(tmpdt.Rows[i]["节点名称"]);
                    }
                    NodeData tmpnd = new NodeData();
                    tmpnd.NodeID = GetString(tmpdt.Rows[i]["节点ID"]);
                    tmpnd.ParentNodeID = GetString(tmpdt.Rows[i]["父节点ID"]);
                    tmpnd.NodeName = GetString(tmpdt.Rows[i]["节点名称"]);
                    tmpnd .LastLogic = GetString(tmpdt.Rows[i]["节点逻辑"]);
                    tmpnd .Field = GetString(tmpdt.Rows[i]["字段"]);
                    tmpnd .FieldLogic = GetString(tmpdt.Rows[i]["字段逻辑"]);
                    tmpnd .Value = GetString(tmpdt.Rows[i]["值"]);
                    tmptn.Tag = tmpnd;
                    if (tnParentNode != null)
                    {
                        tnParentNode.Nodes.Add(tmptn);
                    }
                    else
                    {
                        tvLogics.Nodes.Add(tmptn);
                    }
                    AddNodes(tmptn, strNodeID);
                }
            }
        }

        private bool GetBoolean(object tmpobj)
        {
            try
            {
                return Convert.ToBoolean(tmpobj);
            }
            catch
            {
                return false;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void tvLogics_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null)
            {

            }
        }
    }
}